//
//  DialogWindowController.m
//  iSwitchView
//
//  Created by Ronnie on 08-11-30.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "DialogWindowController.h"


@implementation DialogWindowController
- (id)init
{
    if (![super initWithWindowNibName:@"DialogWindow"])
        return nil;
	chatFriendData = nil;
	NSMutableParagraphStyle * aParagraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
	receiveMSMTitle = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
											  [NSColor blueColor],NSForegroundColorAttributeName,
											  [NSFont systemFontOfSize:12.0],NSFontAttributeName,
											  aParagraphStyle, NSParagraphStyleAttributeName,
											  nil];
	sendMSMTitle = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
					  [NSColor colorWithCalibratedRed:0.0 green:0.5 blue:0.0 alpha:1.0],NSForegroundColorAttributeName,
					  [NSFont systemFontOfSize:12.0],NSFontAttributeName,
					  aParagraphStyle, NSParagraphStyleAttributeName,
					  nil];
	msmContent = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
				   [NSColor blackColor],NSForegroundColorAttributeName,
				   [NSFont systemFontOfSize:14.0],NSFontAttributeName,
				   aParagraphStyle, NSParagraphStyleAttributeName,
				   nil];
	AlarmContent = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
					 [NSColor redColor],NSForegroundColorAttributeName,
					 [NSFont systemFontOfSize:14.0],NSFontAttributeName,
					 aParagraphStyle, NSParagraphStyleAttributeName,
					 nil];
	sendMSMTo = SEND_MSM_TO_PHONE;
	return self;
}

- (void) dealloc
{
	NSLog(@"release dialog window");
	if (sendMSMTo == SEND_MSM_TO_COMPUTER) {
		fx_end_dialog([[chatFriendData getUserInfo] userID]);
	}
	[chatFriendData release];
	
	[receiveMSMTitle release];
	[sendMSMTitle release];
	[msmContent release];
	[AlarmContent release];
	[super dealloc];
}


- (IBAction)sendMSM:(id)sender
{
	if (sendMSMTo == SEND_MSM_TO_COMPUTER) {
		fx_dialog_send([[chatFriendData getUserInfo] userID], [[inputField stringValue] UTF8String], NULL, NULL);
	}
	else
	{
		fx_send_sms ([[chatFriendData getUserInfo] userID], [[inputField stringValue] UTF8String], NULL,NULL);
	}
	NSAttributedString* sendMSMUp = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:\n",@"Me"] attributes:sendMSMTitle];
	NSAttributedString* sendMSMDown = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n",[inputField stringValue]] attributes:msmContent];
	@synchronized(self)
	{
		[[outputField textStorage] appendAttributedString:sendMSMUp];
		[[outputField textStorage] appendAttributedString:sendMSMDown];
		
		[outputField scrollRangeToVisible:NSMakeRange([[outputField textStorage] length], 0)];
		
		[chatFriendData addAttributedString:sendMSMUp];
		[chatFriendData addAttributedString:sendMSMDown];
		NSLog(@"%@", [inputField stringValue]);
		[inputField setStringValue:@""];
	}
}

- (void)getAMSM:(NSString*)msg
{
	NSLog(@"getAMSM = %@", msg);
	
	NSAttributedString* getMSMUp = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:\n",[[chatFriendData getUserInfo] userName]] attributes:receiveMSMTitle];
	NSAttributedString* getMSMDown = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n",msg] attributes:msmContent];

	@synchronized(self)
	{
			[[outputField textStorage] appendAttributedString:getMSMUp];
			[[outputField textStorage] appendAttributedString:getMSMDown];
		
			[outputField scrollRangeToVisible:NSMakeRange([[outputField textStorage] length], 0)];
			
		[chatFriendData addAttributedString:getMSMUp];
		[chatFriendData addAttributedString:getMSMDown];
	}
	[NSApp requestUserAttention:NSCriticalRequest];
}

- (void)setChatFriendData:(FXTreeNode*)node
{
	chatFriendData = [node retain];
	[self setSendDstByStatus:[[chatFriendData getUserInfo] userState]];
	[[self window] setTitle:[NSString stringWithFormat:@"Chat with %@", [[node getUserInfo] userName]]];
	[self loadHistoryRecord];
	
}

- (void)reloadFriendStatur
{
	[self setSendDstByStatus:[[chatFriendData getUserInfo] userState]];
}

- (void)setSendDstByStatus:(int)status
{
	switch (status)
	{
		case FX_STATUS_ONLINE:
		case FX_STATUS_BUSY:
		case FX_STATUS_MEETING:
		case FX_STATUS_PHONE:
		case FX_STATUS_AWAY:
		case FX_STATUS_DINNER:
		case FX_STATUS_EXTENDED_AWAY:
			if (sendMSMTo == SEND_MSM_TO_PHONE) {
				fx_begin_dialog([[chatFriendData getUserInfo] userID], NULL, NULL);
			}
			sendMSMTo = SEND_MSM_TO_COMPUTER;
			[statusHint setStringValue:[NSString stringWithFormat:@"%@ is online, message will be sent to computer.", [[chatFriendData getUserInfo] userName] ]];
			break;
		case FX_STATUS_OFFLINE:
		case FX_STATUS_MOBILE:
			if (sendMSMTo == SEND_MSM_TO_COMPUTER) {
				fx_end_dialog([[chatFriendData getUserInfo] userID]);
			}
			[statusHint setStringValue:[NSString stringWithFormat:@"%@ isn't online, message will be sent to phone.", [[chatFriendData getUserInfo] userName] ]];
			sendMSMTo = SEND_MSM_TO_PHONE;
			break;
		default:
			if (sendMSMTo == SEND_MSM_TO_PHONE) {
				fx_begin_dialog([[chatFriendData getUserInfo] userID], NULL, NULL);
			}
			[statusHint setStringValue:[NSString stringWithFormat:@"%@ is onlie, message will be sent to computer.", [[chatFriendData getUserInfo] userName] ]];
			sendMSMTo = SEND_MSM_TO_COMPUTER;
			break;
	}
}

- (void)loadHistoryRecord
{
	if (chatFriendData) {
	NSLog(@"reload history, %i", [[chatFriendData getChatHistory] count]);
		NSAttributedString* obj;
		for (obj in [chatFriendData getChatHistory]) {
			[[outputField textStorage] appendAttributedString:obj];
		}
	}
	else
	{
		NSLog(@"chat data not set");
	}
}

- (BOOL)windowShouldClose:(id)sender {	
	// if hide on close
	[chatFriendData releaseDialogWindowController];
	//[[self window] orderOut:self];
	return YES;
}

@end
